package core;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DataFromWeatherWebPage {
	private static String codePage = "UTF-8";

	public	DataFromWeatherWebPage (String date){
		this.setDate(date);
	}	
	private static String date =  "2010/5/16";

	public static String pageAddress="http://www.wunderground.com/history/airport/LFMN/"+ getDate() +"/DailyHistory.html?req_city=NA&req_state=NA&req_statename=NA&MR=1";


	public String[] getContentOfHTTPPage(String pageAddress, String codePage) throws Exception {
		StringBuilder sb = new StringBuilder();
		URL pageURL = new URL(pageAddress);
		URLConnection uc = pageURL.openConnection();
		BufferedReader br = new BufferedReader(
				new InputStreamReader(
						uc.getInputStream(), codePage));
		try {
			String inputLine;
			while ((inputLine = br.readLine()) != null) {
				sb.append(inputLine);
			}         
		} finally {
			br.close();
		}
		
		String pageWebContent = sb.toString();
	//	String temperatureMoyen = pageWebContent.substring(17866,17868);
	//	String humidityMoyen = pageWebContent.substring(19149,19151);
		String temperatureMoyen = pageWebContent.substring(17850,17880);
		String humidityMoyen = pageWebContent.substring(19140,19160);
		Pattern p = Pattern.compile("-?\\d+");
        Matcher m = p.matcher(temperatureMoyen);
        Pattern p1 = Pattern.compile("-?\\d+");
        Matcher m1 = p1.matcher(humidityMoyen);
        
	//	System.out.println(temperatureMoyen);
		
//		System.out.println(humidityMoyen);
		String[] dataFromWeb = new String [2];
		m.find(); m1.find();
		dataFromWeb[0] = m.group();
		dataFromWeb[1] = m1.group();
		return  dataFromWeb;
	}
	
	public static void main(final String[] args) throws Exception {
		DataFromWeatherWebPage gaga = new DataFromWeatherWebPage(date);
		String[]toto = gaga.getContentOfHTTPPage("http://www.wunderground.com/history/airport/LFMN/"
		+ getDate() +"/DailyHistory.html?req_city=NA&req_state=NA&req_statename=NA&MR=1", getCodePage());
		System.out.println(toto[0]+"  "+ toto[1]);
	}

	public static String getDate() {
		return date;
	}
	public void setDate(String date) {
		DataFromWeatherWebPage.date = date;
	}
	public static String getCodePage() {
		return codePage;
	}
	public static void setCodePage(String codePage) {
		DataFromWeatherWebPage.codePage = codePage;
	}
}